<?php

namespace app\company\controller;

use app\company\validate\CompanyOrderStorageValidate;
use app\service\traits\Crud;
use app\company\model\CompanyOrderStorage as CompanyOrderStorageModel;

class CompanyOrderStorage extends Base
{
    private CompanyOrderStorageModel $model;

    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model         = new CompanyOrderStorageModel();
        $this->validateClass = CompanyOrderStorageValidate::class;
        $this->field         = 'id,warehouse_area_id,warehouse_storage_name,warehouse_id,goods_id,goods_rule_id,quantity,production_time,active_time,warehouse_storage_id,orig_quantity';
        $this->withSearch    = 'goods_id,goods_rule_id,warehouse_id,warehouse_area_id,warehouse_storage_id,company_id,company_order_id,company_order_detail_id,production_time,active_time';
        $this->isDel         = true;
        $this->orderSort     = ['quantity' => 'desc'];
    }


    public function format($list): mixed
    {
        return $list->each(function ($item) {
            $item->warehouse_name      = $item->warehouse()->value('name');//得到仓库名称
            $item->warehouse_area_name = $item->warehouseArea()->value('name');//得到库区名称
            $item->goods               = $item->goods()->withTrashed()->field('name,unit,img')->find();//得到商品名称/单位/图片
            $item->goods->spec_name    = $item->goodsRule()->withTrashed()->value('spec_name');//得到商品规格
            $item->active_time         = setDates($item->active_time);//批次时间
            $item->production_time     = setDates($item->production_time);//生产时间
        });
    }

}